package org.gc.contact.fetcher;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gc.contact.exception.ExceptionMessage;
import org.gc.contact.exception.ProtocolException;
import org.gc.contact.http.HttpBrowser;
import org.gc.contact.util.RegexpUtil;

public abstract class AbstractContactFetcher implements IContactFetcher {
	
	protected final Log logger = LogFactory.getLog(getClass());
	protected final HttpBrowser browser;
	
	protected String username;
	protected String password;
	
	public AbstractContactFetcher(String username, String password) {
		this.username = username;
		this.password = password;
		browser = new HttpBrowser();
	}
	
	protected String getJSRedirection(String htmlContent) throws ProtocolException {
		Pattern p = RegexpUtil.getJSRedirectionRegexp();
		Matcher matcher = p.matcher(htmlContent);
	    if (!matcher.find()) {
	    	String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=JSRedirection,Regexp=" + p.pattern() + "]";
	        logger.error(msg);
	        throw new ProtocolException(msg);
	    }
	    if (logger.isDebugEnabled()) {
	    	logger.debug("Redirection URL = " + matcher.group(1));
	    }
	    return matcher.group(1);
	}
	
	protected String getFormInputValue(String name, String htmlContent) throws ProtocolException {
	    Pattern p = RegexpUtil.getFormInputValueRegexp();
	    int index = htmlContent.indexOf(name);
	    int prePos = index - 400 > 0 ? index - 400 : 0;
	    int postPos = index + 400 < htmlContent.length() ? index + 400 : htmlContent.length();
	    int start = htmlContent.substring(prePos, index).lastIndexOf("<input") + prePos;
	    int end = htmlContent.substring(index, postPos).indexOf(">") + index;
	    String htmlInput = htmlContent.substring(start, end + 1);
	    if (logger.isDebugEnabled()) {
	    	logger.debug("HtmlInput=" + htmlInput);
	    }
	    Matcher matcher = p.matcher(htmlInput);
	    if (!matcher.find()) {
	    	String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=FormInputValue,Regexp=" + p.pattern() + "]";
	        logger.error(msg);
	        throw new ProtocolException(msg);
	    }
	    if (logger.isDebugEnabled()) {
	    	logger.debug("Name = " + name + ";value = " + matcher.group(1));
	    }
	    return matcher.group(1);
	}
	
	protected String getHost(String url) throws ProtocolException {
		Pattern p = RegexpUtil.getHostFromURIRegexp();
		Matcher matcher = p.matcher(url);
	    if (!matcher.find()) {
	    	String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=Host,Regexp=" + p.pattern() + "]";
	        logger.error(msg);
	        throw new ProtocolException(msg);
	    }
	    if (logger.isDebugEnabled()) {
	    	logger.debug("Host = " + matcher.group(1));
	    }
	    return matcher.group(1);
	}
	
	protected List<String> getValue(Pattern p, String htmlContent) throws ProtocolException {
		List<String> data = new ArrayList<String>();
		Matcher matcher = p.matcher(htmlContent);
	    while (matcher.find()) {
	    	try {
	    		data.add(matcher.group(1));
	    		if (logger.isDebugEnabled()) {
	    			logger.debug("Matched String=" + matcher.group(1));
	    		}
	    	} catch (Exception e) {
	    		String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=getValue,Regexp=" + p.pattern() + "]";
		        logger.error(msg);
		        throw new ProtocolException(msg);
	    	}
	    }
	    if (data.size() == 0) {
	    	String msg = ExceptionMessage.PROTOCOL_MSG + "[Type=getValue,Regexp=" + p.pattern() + "]";
	    	logger.warn(msg);
	    }
	    return data;
	}
}
